package jude.java.money.black;

import java.io.IOException;
import java.net.ConnectException;
import java.net.SocketException;
import java.net.UnknownHostException;

import jude.java.money.beans.FtpSite;
import jude.java.money.provide.Provider;
import jude.java.money.spring.Services;
import jude.java.money.task.BaseTask;
import jude.java.money.task.TaskContext;
import jude.java.money.tools.PageRank;
import jude.java.util.I;

import org.apache.commons.net.ftp.FTPClient;

public class LoginTask extends BaseTask<FtpSite> {
	private static final int TIMEOUT = 30000;	//30s
	
	public LoginTask(Provider<FtpSite> provider) {
		super(provider);
	}

	@Override
	protected void execute(FtpSite site) {
		TaskContext.signal(1, site.getHost());
		site.setLoginTimes(0);
		site.setLogined(0);
		site.tryLogin(true);
		I.get("total").increase();
		try {
			FTPClient ftp = getFtp(site);
			if (ftp.login(site.getUsr(), site.getPwd())) {
				site.onLogined();
				site.setError(null);
				System.out.print("++");
				System.out.println(site.getHost());
				TaskContext.signal(2);
				I.get("success").increase();
			} else {
				site.setError(null);
				System.out.print("  ");
				System.out.println(site.getHost());
				TaskContext.signal(3);
				I.get("fail").increase();
			}
			ftp.disconnect();
		} catch (UnknownHostException e) {
//			System.out.println(e.getClass().getName() + "\t" + site.getHost());
			site.setError("UnknownHostException");
			I.get("unknownHost").increase();
		} catch (ConnectException e) {
//			e.printStackTrace();
			System.out.println(e.getClass().getName() + "\t" + site.getHost() + "\t" + e.getMessage());
			site.setError(e.getClass().getSimpleName() + " " + e.getMessage());
			I.get("connectE").increase();
		} catch (SocketException e) {
//			e.printStackTrace();
			System.out.println(e.getClass().getName() + "\t" + site.getHost() + "\t" + e.getMessage());
			site.setError(e.getClass().getSimpleName() + " " + e.getMessage());
			I.get("socketE").increase();
		} catch (java.net.SocketTimeoutException e) {
			System.out.println(e.getClass().getName() + "\t" + site.getHost() + "\t" + e.getMessage());
			site.setError(e.getClass().getSimpleName() + " " + e.getMessage());
			I.get("socketT").increase();
		} catch (org.apache.commons.net.ftp.FTPConnectionClosedException e) {
			System.out.println(e.getClass().getName() + "\t" + site.getHost() + "\t" + e.getMessage());
			site.setError(e.getClass().getSimpleName() + " " + e.getMessage());
			I.get("closed").increase();
		} catch (Exception e) {
			e.printStackTrace();
			site.setError(e.getClass().getName() + ":" + e.getMessage());
			System.out.print("--");
			System.out.println(site.getHost());
			I.get("error").increase();
		}
		if (site.getPr() < -1) {
			int pr = PageRank.getPageRank("http://" + site.getHost());
			if (pr != -2) {
				site.setPr(pr);
			} else if (site.getPr() == -3) {
				site.setPr(pr);
			}
		}
		Services.getGlobalService().update(site);
	}

	protected FTPClient getFtp(FtpSite site) throws IOException {
		FTPClient ftp = new FTPClient();
		ftp.setConnectTimeout(TIMEOUT);
		ftp.setDataTimeout(TIMEOUT);
		ftp.setDefaultTimeout(TIMEOUT);
		ftp.connect(site.getHost(), site.getPort());
		ftp.setSoTimeout(TIMEOUT);
		ftp.setDataTimeout(TIMEOUT);
		return ftp;
	}
}
